package model.dba;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.IRCursadaInstructor;
import model.IRHorarioCursada;


public class IRCursadaInstructorDAO extends JDBC{
	private static IRCursadaInstructorDAO iRCursadaInstructorDAO;

	private IRCursadaInstructorDAO() {
		super();
	}

	public static IRCursadaInstructorDAO getInstance() {
		if (iRCursadaInstructorDAO == null) {
			iRCursadaInstructorDAO = new IRCursadaInstructorDAO();
		}
		return iRCursadaInstructorDAO;
	}

	public int add(IRCursadaInstructor iRcursadaInstructor) {
		try {

			String queryString = "INSERT INTO iRcursadaInstructor(idCursadaC,dniInstructorC)" + " VALUES(?,?) ";
			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setLong(1, iRcursadaInstructor.getidCursada());
			preparedStatement.setString(2,iRcursadaInstructor.getdniInstrctor());

			preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return getLastId();

	}

	// En un futuro se puede poner nombre

	public boolean update(IRCursadaInstructor iRcursadaInstructor) {
		boolean state = false;
		try {
			String queryString = "UPDATE iRcursadaInstructor SET idCursadaC=?, dniInstructorC=? WHERE idHorarioC=?";

			preparedStatement = connection.prepareStatement(queryString);

			preparedStatement.setLong(1, iRcursadaInstructor.getidCursada());
			preparedStatement.setString(2, iRcursadaInstructor.getdniInstrctor());
			preparedStatement.setLong(3, iRcursadaInstructor.getidCursada());


			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			}

			catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return state;
	}

	public boolean delete(Integer idCursadaC) {
		boolean state = false;
		try {
			String queryString = "DELETE FROM iRcursadaInstructor WHERE idCursadaC=?";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setInt(1, idCursadaC);
			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return state;

	}

	public IRCursadaInstructor get(Integer idCursadaC) {

		IRCursadaInstructor iRcursadaInstructor = new IRCursadaInstructor();
		try {
			String queryString = "SELECT * FROM iRcursadaInstructor where idCursadaC = '" + idCursadaC + "';";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				iRcursadaInstructor = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRcursadaInstructor;
	}

	// En un futuro se puede poner nombre

	public IRCursadaInstructor find(Integer dniInstructor) {

		IRCursadaInstructor iRcursadaInstructor = new IRCursadaInstructor();
		try {
			String queryString = "SELECT * FROM iRcursadaInstructor where dniInstructorC ='" + dniInstructor + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				iRcursadaInstructor = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRcursadaInstructor;
	}

	public int getLastId() {
		int ret = 0;
		try {
			String queryString = "SELECT idCursadaC FROM iRcursadaInstructor order by idCursadaC DESC LIMIT 1  ";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ret = resultSet.getInt("idCursadaC");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return ret;

	}

	public List<IRCursadaInstructor> getAll() {
		List<IRCursadaInstructor> iRcursadaInstructorList = new ArrayList<IRCursadaInstructor>();

		try {
			String queryString = "SELECT * FROM iRcursadaInstructor ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				iRcursadaInstructorList.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRcursadaInstructorList;
	}
	
	public List<IRCursadaInstructor> findAll(String dniInstructor) {

		List<IRCursadaInstructor> areaList = new ArrayList<IRCursadaInstructor>();
		try {
			String queryString = "SELECT * FROM iRcursadaInstructor where dniInstructorC = '" + dniInstructor + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				areaList.add(populate());

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return areaList;
	}


	private IRCursadaInstructor populate() throws SQLException {

		IRCursadaInstructor iRcursadaInstructor = new IRCursadaInstructor();
		iRcursadaInstructor.setidCursada((int) resultSet.getLong("idCursadaC"));
		iRcursadaInstructor.setdniIntructor(resultSet.getString("dniInstructorC"));
		return iRcursadaInstructor;

	}
}
